<?php
namespace app\index\controller\xilujob;


use app\common\controller\xilujob\Xilujob;
use app\common\model\Attachment;
use app\common\model\xilujob\CompanyStatistics;
use app\common\model\xilujob\EnterpriseInfo;
use app\common\model\xilujob\Job;
use app\common\model\xilujob\JobApply;
use app\common\model\xilujob\JobInterview;
use app\common\model\xilujob\Resume;
use fast\Date;
use think\Db;

class Console extends Xilujob
{
    protected $noNeedRight = ['*'];



    /**
     * 查看
     */
    public function index()
    {

        $enterpriseInfo = EnterpriseInfo::where('company_id',$this->company->id)->field('resume_action_num,job_num_limit,interview_num,job_refresh_num')->find();

        $applyCount = JobApply::where('company_id', $this->company->id)
            ->where('status',0)
            ->whereTime('createtime','w')
            ->count('distinct user_id');
        $interviewCount = JobApply::where('company_id', $this->company->id)
            ->where('status',4)
            ->count('distinct user_id');
        $applyMonth = JobApply::where('company_id', $this->company->id)
            ->whereTime('createtime','m')
            ->count('distinct user_id');
        $interviewMonth = JobInterview::where('company_id', $this->company->id)
            ->whereBetween('interview_time',[strtotime('tomorrow'), strtotime('tomorrow')+86399])
            ->count('distinct user_id');


        $this->view->assign('enterpriseInfo', $enterpriseInfo);
        $this->view->assign('new_apply_count', $applyCount);
        $this->view->assign('need_interview_count',$interviewCount);
        $this->view->assign('apply_month', $applyMonth);
        $this->view->assign('interview_month', $interviewMonth);

        #统计图
        $format = '%Y-%m-%d';

        $starttime = Date::unixtime('day', -6);
        $endtime = Date::unixtime('day', 0, 'end');
        //1.投递趋势-最近七天
        $job_apply = JobApply::field("COUNT(id) as num,from_unixtime(createtime, '{$format}') AS dates")->where('createtime','between',[$starttime,$endtime])->group('dates')->select();
        $diff_day = ceil(($endtime-$starttime)/86400);
        $job_apply_count = [];
        $array = $job_apply ? array_column(collection($job_apply)->toArray(),'num','dates') : [];

        #面试数
        $job_interview = JobInterview::field("COUNT(id) as num,from_unixtime(interview_time, '{$format}') AS dates")->where('interview_time','between',[$starttime,$endtime])->group('dates')->select();
        $job_interview_count = [];
        $array2 = $job_interview ? array_column(collection($job_interview)->toArray(),'num','dates') : [];

        for ($i=0;$i<$diff_day;$i++){
            $date = date('Y-m-d',$starttime+$i*86400);
            $job_apply_count[$date] = isset($array[$date]) ? $array[$date] :mt_rand(10,100);
            $job_interview_count[$date] = isset($array2[$date]) ? $array2[$date] :mt_rand(10,100);
        }
        $job_view_num = Job::sum('view_count');
        $job_apply_num = JobApply::count();
        $job_interview_num = JobInterview::count();
        $job_interview_employee_num = JobInterview::where('status',4)->count();
        $job_unfit_num = JobApply::where('status',1)->count() + JobInterview::whereIn('status',[1,3])->count();

        $this->view->assign([
            'attachmentnums'  => Attachment::count(),
            'attachmentsize'  => Attachment::sum('filesize'),
            'picturenums'     => Attachment::where('mimetype', 'like', 'image/%')->count(),
            'picturesize'     => Attachment::where('mimetype', 'like', 'image/%')->sum('filesize'),
            'job_apply'       => $job_apply_count,
            'job_interview'   => $job_interview_count,
            'job_num'         => [
                ['name'=>'访问数','value'=>$job_view_num],
                ['name'=>'投递数','value'=>$job_apply_num],
                ['name'=>'面试数','value'=>$job_interview_num],
                ['name'=>'录用数','value'=>$job_interview_employee_num],
                ['name'=>'不合适','value'=>$job_unfit_num],
            ]
        ]);


        $this->view->assign('title','控制台');
        return $this->view->fetch();
    }

}